package com.hongye.proprietary.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongye.proprietary.entity.GroupBuyingEntity;
import com.hongye.proprietary.service.GroupBuyingService;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.R;

import javax.validation.Valid;
import javax.validation.executable.ValidateOnExecution;


/**
 * 团购表
 *
 * @author lins
 * @email 695565697@qq.com
 * @date 2021-10-14 11:16:47
 */
@RestController
@RequestMapping("proprietary/groupbuying")
public class GroupBuyingController {
    @Autowired
    private GroupBuyingService groupBuyingService;





    /*
    开团功能
     */
    /**
     * 根据团购表得id去查当前需要进行开团操作得内容
     */
    @RequestMapping("/info/{groupBuyingId}")
    public R info(@PathVariable("groupBuyingId") Integer groupBuyingId){
        GroupBuyingEntity groupBuying = groupBuyingService.getById(groupBuyingId);

        return R.ok().put("groupBuying", groupBuying);
    }

    /*
    根据id回显之后进行修改操作
     */
    @RequestMapping("/updatestartagroup")
    public R updatestartagroup(@RequestBody GroupBuyingEntity groupBuyingEntity){
        /*
            把团购状态设置为已开团
         */
        groupBuyingEntity.setGroupBuyingStatus(1);
        /*
        再开团表里面添加内容
         */
        groupBuyingService.updateById(groupBuyingEntity);

        return R.ok("开团成功");
    }


    /**
     * 团购详情
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = groupBuyingService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 创建团购
     */
    @RequestMapping("/save")
    public R save(@RequestBody @Validated GroupBuyingEntity groupBuying){
		groupBuyingService.save(groupBuying);

        return R.ok();
    }



    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] groupBuyingIds){
		groupBuyingService.removeByIds(Arrays.asList(groupBuyingIds));

        return R.ok();
    }

}
